import DataMgr from "../../../general/Script/mgr/dataManager";
import BlockGmMgr from "./blockGMMgr";
import { BlockState, GameType, touchNodeLength } from "./content";


const gameRow = 8;
const gameCol = 8;


export default class BlockGMData{
    node:cc.Node;
    mgr:BlockGmMgr;
    map : string[][] = [];

    score:number = 0;
    wantAddScore:number = 0;
    combTimes:number = 0;
    addComb:boolean = false;
    highScore:number = 0;   
    touchArr:number[] = [0,0,0];
    diamondArr:string[][] = [];
    needTarget:number[] = [];

    constructor(node:cc.Node,mgr:BlockGmMgr){
        this.node = node;
        this.mgr = mgr;
    }



   
    




    initMap(){
        for(let i = 0; i < gameRow; i++){
            this.map[i] = [];
            for(let j = 0; j < gameCol; j++){
                this.map[i][j] = `${BlockState.none}_1`//Math.floor(Math.random() * 7) ;
            }
           
        }
    }


    initGameTarget(target){
        this.needTarget = target.slice(0);
        // console.log('target = ',this.needTarget);
    }
   


    randomMap(){
        for(let i = 0; i < gameRow; i++){
            this.map[i] = [];
            for(let j = 0; j < gameCol; j++){
                this.map[i][j] = `${BlockState.show}_${Math.floor(Math.random() * 7)}` ;
            }
        }
    }



    newTouchBlock(isReGame = false){
        
        if(isReGame){   

           this.combTimes = 0;
           this.addComb = false
            this.touchArr = [0,0,0];
           return this.touchArr;
        }


        const arr = [0,0,0];

        for(let i = 0; i < 3; i++){
            arr[i] = Math.floor(Math.random() * this.mgr.blockTouches.length) ;
        }

        if(!this.addComb){
            this.combTimes = 0; 
        }
        this.addComb = false;
        this.touchArr = arr;
        if(this.mgr.gameType == GameType.star){
            this.addDiamond();
        }
       
        return arr;

       
    }

    addDiamond(){
        this.diamondArr = [[],[],[]];
        for(let i in this.touchArr){
            const blockCount = touchNodeLength[this.touchArr[i]];

            //随机一个数小于blockCount

            let randomTimes = Math.floor(Math.random() * blockCount)+1;
            if(randomTimes>3){
                randomTimes =  Math.min(blockCount-3,randomTimes)
            }else{
                randomTimes = 1;
            }
            



            const indexArr = this.getRandomNumbers(0,blockCount-1,randomTimes);

            for(let j = 0;j<indexArr.length;j++){
                const id = indexArr[j];
                const type = Math.floor(Math.random() * 4);
                const info = `${id}_${type}`;
                this.diamondArr[i].push(info);
            }
        }

        // console.log('diamond = ',this.diamondArr);
        // console.log('diamondStr = ',this.mapToStr(this.diamondArr));
    }

    getRandomNumbers(start, end,count) {
        const numbers = [];
        for (let i = start; i <= end; i++) {
          numbers.push(i);
        }
    
        //打乱 numbers 数组
    
    
        for (let i = 0; i < numbers.length; i++) {
          const  randomIndex = Math.floor(Math.random() * numbers.length);
          const  temp = numbers[i];
          numbers[i] = numbers[randomIndex];
          numbers[randomIndex] = temp;
        }
    
        return numbers.splice(0,count);
    }

    changeTouchBlock(index:number){
        this.touchArr[index] = -1;
        this.diamondArr[index] = [];
    }


    saveMap(){
        if(this.mgr.gameType != GameType.normal){
            return;
        }
        const mapInfo= {
            map:this.mapToStr(this.map),
            touchArr:this.touchArr,
            // diamondArr:this.mapToStr(this.diamondArr),
            score:this.score,
            combTimes:this.combTimes,
            addComb:this.addComb,
        }


       

        DataMgr.Ins().setLocalStorage('block_mapInfo',mapInfo);
    }

    mapToStr(map){
        let mapStrArr = [];

        for(let i = 0;i<map.length;i++){
            const mapRow = map[i].join(',');
            mapStrArr.push(mapRow);
        }
        return mapStrArr.join(':');
    }

    strToMap(mapStr){
        let mapStrArr = mapStr.split(':');
        let map = [];
        for(let i = 0;i<mapStrArr.length;i++){
            const mapRow = mapStrArr[i].split(',');
            map.push(mapRow);
        }
        return map;
    }

    initMapFromLocal(){
        if(this.mgr.gameType != GameType.normal){
            return false;
        }
        const mapInfo = DataMgr.Ins().getLocalStorage('block_mapInfo');
        if(mapInfo){
            this.map = this.strToMap(mapInfo.map);
            this.touchArr = mapInfo.touchArr;
            // this.diamondArr = this.strToMap(mapInfo.diamondArr);
            this.score = mapInfo.score;
            this.combTimes = mapInfo.combTimes;
            this.addComb = mapInfo.addComb;
           // console.log('this.map = ',this.map);
            return true;
        }
        return false;
    }

    setMap(map:string){
        this.map = this.strToMap(map);
    }

    delMapInfo(){
        if(this.mgr.gameType != GameType.normal){
            return;
        }
        DataMgr.Ins().removeLocalStorage('block_mapInfo');
    }



    setAddScore(row,col){
        const base = 10;
        const add = 10;
        const total = row+col;

        const score = base*total;
        this.wantAddScore = score;

    }

    setScore(score){
        this.score = score;
    }   
    
    getScore(){
        return this.score;
    }


    addScore(score){    
        this.score += score;
    }

    getMap(){
        return this.map;
    }

    changeMap(row,col,state,type){
        this.map[row][col] = `${state}_${type}`;
    }


    clearMapRow(row){
        for(let i = 0; i < gameCol; i++){
            this.map[row][i] = `${BlockState.none}_1`;
        }
    }

    clearMapCol(col){

        for(let i = 0; i < gameRow; i++){
            this.map[i][col] =  `${BlockState.none}_1`;;
        }
    }   

    clearMap(){
        for(let i = 0; i < gameRow; i++){
            for(let j = 0; j < gameCol; j++){
                this.map[i][j] =  `${BlockState.none}_1`;
            }
        }
    }

    setCombScore(){
       // console.log('comb = ',this.combTimes,this.wantAddScore)
        this.wantAddScore = this.combTimes*this.wantAddScore;
    }

    getCombTimes(){
        return this.combTimes;
    }


    addCombTimes(){
        this.combTimes++;
        this.addComb = true;
       // console.log('comb2222 = ',this.combTimes,this.wantAddScore)
    }

    clearCombTimes(){
        this.combTimes = 0;
    }   



    //计分模式

    getLeftScore(){
        return this.needTarget[0]
    }

    reduceLeftScore(score){
        this.needTarget[0]-=score;

        if(this.needTarget[0]<=0){
            return true
        }

        return false
    }


    //步数模式
    getLeftStep(){
        return this.needTarget[0]
    }

    reduceLeftStep(){
        this.needTarget[0]-=1;
        this.needTarget[0] = Math.max(0,this.needTarget[0]);
        if(this.needTarget[0]<=0){
            return true
        }

        return false;
    }


    //星星模式
    getLeftStar(){
        return this.needTarget;
    }

    reduceLeftStar(type){
        this.needTarget[type]-=1;
        this.needTarget[type] = Math.max(0,this.needTarget[type]);

        for(let i in this.needTarget){
            if(this.needTarget[i]>0){
                return false;
            }
        }

        return true;
    }


}